package com.gqzdev.xblog.controller.home;


import com.gqzdev.xblog.entity.Description;
import com.gqzdev.xblog.entity.News;
import com.gqzdev.xblog.service.AdService;
import com.gqzdev.xblog.service.DescriptionService;
import com.gqzdev.xblog.service.NewsCategoryService;
import com.gqzdev.xblog.service.NewsService;
import com.gqzdev.xblog.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 前台页面首页控制器
 *
 * @author ganquanzhong
 */

@Controller
public class IndexController {

	@Autowired
	private NewsCategoryService newsCategoryService;

	@Autowired
	private NewsService newsService;

	@Autowired
	private AdService adService;

	@Autowired
	private DescriptionService descService;
	
	@Autowired
	private WebUtil webUtil;

	/**
	 * 系统首页
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/",method= RequestMethod.GET)
	public ModelAndView index(ModelAndView model, HttpServletRequest request){
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("offset", 0);
		queryMap.put("pageSize", 10);
		model.addObject("newsCategoryList", newsCategoryService.findAllShow());
		List<News> newsList = newsService.findListNoDel(queryMap);
		for (News news:newsList){
			webUtil.removeHtml(news);
			if (news.getPhoto().equals("/image/news-pic.jpg")){
				news.setPhoto(null);
			}
		}
		model.addObject("newsList", newsList);
		// 轮播图
		Map<String, Object> queryAdMap = new HashMap<String, Object>();
		queryAdMap.put("type", 1);
		queryAdMap.put("offset", 0);
		queryAdMap.put("pageSize", 3);
		model.addObject("slideAdList", adService.findListShow(queryAdMap));

		// 获取网站信息  保存到session中
		request.getSession().setAttribute("header_topbar",descService.queryById(1));
		request.getSession().setAttribute("logo_content",descService.queryById(2));
		request.getSession().setAttribute("about",descService.queryById(5));
		request.getSession().setAttribute("tag",descService.queryById(6));

		model.setViewName("home/index/index");
		return model;
	}

	/**
	 * 获取网站基本信息
	 * @return
	 */
	@RequestMapping(value="/get_site_info",method= RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getSiteInfo(){
		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put("type", "success");
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("offset", 0);
		queryMap.put("pageSize", 99999);
		//文章总数
		retMap.put("totalArticle", newsService.getTotalFront(queryMap));
		retMap.put("siteDays", getDays("2018-03-27"));
		return retMap;
	}

	/**
	 * 计算时间，转为多少天
	 */
	private long getDays(String data){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date startDate = null;
		try {
			startDate = sdf.parse(data);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Date endDate = new Date();
		long time = (endDate.getTime() - startDate.getTime())/1000/3600/24;
		return time;
	}
}
